package persistence.implementation;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import model.Payment;
import model.Person;
import model.PersonBasis;
import model.PersonGroup;
import persistence.interfaces.Database;

public final class CacheDatabase extends Database {
	public static List<Person> personList = new ArrayList<Person>();
	public static List<PersonGroup> personGroupList = new ArrayList<PersonGroup>();
	public static List<Payment> paymentList = new ArrayList<Payment>();
	
	@Override
	public List<Payment> getPaymentsByGiver(Person giver) {
		return paymentList.stream()
				.filter(b->b.getGiver().getName().equals(giver.getName()))
				.collect(Collectors.toList());
	}
	@Override
	public List<Payment> getPaymentsByReceiver(PersonBasis receiver) {
		return paymentList.stream()
				.filter(b->b.getReceiver().getName().equals(receiver.getName()))
				.collect(Collectors.toList());
	}
	@Override
	public boolean save(Payment payment) {
		paymentList.add(payment);
		return true;
	}
	@Override
	public void delete(Payment payment) {
		if (paymentList.contains(payment)) {
			paymentList.remove(payment);
		}
	}
	@Override
	public boolean exists(Person person) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public Person getPersonByName(String personName) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean save(Person person) {
		personList.add(person);
		return true;
	}
	@Override
	public void delete(Person person) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean exists(PersonGroup personGroup) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public PersonGroup getPersonGroupByName(String personGroupName) {
		Optional<PersonGroup> findFirst = personGroupList.stream()
				.filter(p->p.getName().equals(personGroupName))
				.findFirst();
		if (findFirst.isPresent()) {
			return findFirst.get();
		}
		return null;
	}
	@Override
	public boolean save(PersonGroup personGroup) {
		if (!personGroupList.contains(personGroup)) {
			personGroupList.add(personGroup);
			return true;
		}
		return false;
	}
	@Override
	public void delete(PersonGroup personGroup) {
		// TODO Auto-generated method stub
		
	}
}
